/********************************************************************************
 * Project Name		[JavaSE_Fundamentals]
 * File Name     	[ComboBoxComponent.java]
 * Creation Date 	[2015-01-01]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ********************************************************************************/
package swing.components;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

/**
 * Swing下拉列表框组件
 * 
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 * 
 */
public class ComboBoxComponent {

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				ComboBoxFrame frame = new ComboBoxFrame();
				frame.setVisible(true);
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			}
		});
	}
}

class ComboBoxFrame extends JFrame {

	private static final long serialVersionUID = 4136937774200361091L;

	private JLabel mainLabel = null;
	private JPanel southPanel = null;

	private JComboBox<String> fontComboBox = null;

	public ComboBoxFrame() {
		setTitle("ComboBox Test");
		this.setSize(300, 200);
		setLocationByPlatform(true);
		setResizable(false);

		mainLabel = new JLabel("Hello Java World", SwingConstants.CENTER);

		String[] fontNames = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();

		fontComboBox = new JComboBox<String>(fontNames);
		// fontComboBox.setEditable(true);// 下拉列表框可编辑
		southPanel = new JPanel();
		southPanel.add(fontComboBox);

		fontComboBox.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String fontName = (String) fontComboBox.getSelectedItem();
				int fontMode = Font.PLAIN;
				int fontSize = 20;

				mainLabel.setFont(new Font(fontName, fontMode, fontSize));
			}
		});

		getContentPane().add(mainLabel, BorderLayout.CENTER);
		getContentPane().add(southPanel, BorderLayout.SOUTH);
	}
}
